package com.pdsu.graduation_design.views;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.pdsu.graduation_design.MainActivity;
import com.pdsu.graduation_design.R;
import com.pdsu.graduation_design.adapter.MainSearchListViewAdapter;
import com.pdsu.graduation_design.domain.Commodity;
import com.pdsu.graduation_design.utils.JsonUtil;
import com.pdsu.graduation_design.utils.Result;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.List;

public class Main_Search_Activity extends AppCompatActivity {

    private EditText main_search_et;
    private TextView main_search_tv;
    private ListView main_search_listview;
    private String key;
    private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_search);
        init();
        bind();
    }
    void init(){
        context=this;
        key= getIntent().getStringExtra("data");
        main_search_tv=findViewById(R.id.main_fragment_search_tvb);

        main_search_et=findViewById(R.id.main_fragment_search_et);

        main_search_listview=findViewById(R.id.main_seach_listview);
    }
    void bind(){

        main_search_et.setFocusable(true);
        main_search_et.setFocusableInTouchMode(true);
        main_search_et.setText(key);
        main_search_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                key = main_search_et.getText().toString();
                getData();
            }
        });



    }
    void getData(){
        RequestParams requestParams=new RequestParams(MainActivity.URL+"/commodity/findCommodityByKey");
        requestParams.addParameter("key",key);
        x.http().post(requestParams, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {

                try {
                    Result r= JsonUtil.JsonToObj(result,Result.class);

                    if (r.getCode().equals("200")){

                        List<Commodity> commodities= JsonUtil.json2list(r.getData().toString(),Commodity.class);
                        System.out.println(commodities==null);
                        System.out.println(commodities+"??");
                        if (commodities!=null&&commodities.size()>0){
                            main_search_listview.setAdapter(new MainSearchListViewAdapter(context,commodities));
                            main_search_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                @Override
                                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                    Intent intent = new Intent(context,MainListViewItemDetailActivity.class);
                                    intent.putExtra("cid",commodities.get(position).getCid());
                                    startActivity(intent);
                                }
                            });
                        }else {
                            Toast.makeText(context, "未找到'"+key+"'数据", Toast.LENGTH_SHORT).show();
                        }


                    }


                } catch (Exception e) {
                    e.printStackTrace();
                }


            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });

    }
}